package com.pig.phttprequest;

import java.util.TreeMap;

import org.apache.http.HttpResponse;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;

import com.pig.phttprequest.PHttpRequestCallBack.PHttpDownloadFileCallback;
import com.pig.phttprequest.PHttpRequestCallBack.PHttpStringCallback;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		this.testGet();
		
		this.testPost();
		
		this.testDownload();
		
	}
	
	public void testGet() {
		TreeMap<String, String> params = new TreeMap<String, String>();
		params.put("qqCode", "410289616");
		
		PHttpRequestGet httpGet = new PHttpRequestGet("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline", params);
		httpGet.setHttpStringCallback(new PHttpStringCallback() {
			
			@Override
			public void requestString(String result) {
				// TODO Auto-generated method stub
				System.out.println("testGet result: " + result);
			}
		});
		
		PHttpRequestManager manager = PHttpRequestManager.getInstance();
		manager.runTask(httpGet);
		
	}
	
	public void testPost() {
		TreeMap<String, String> params = new TreeMap<String, String>();
		params.put("qqCode", "410289616");
		
		PHttpRequestPost httpPost = new PHttpRequestPost("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline", params);
		httpPost.setHttpStringCallback(new PHttpStringCallback() {
			
			@Override
			public void requestString(String result) {
				// TODO Auto-generated method stub
				System.out.println("testPost result: " + result);
			}
		});
		
		PHttpRequestManager manager = PHttpRequestManager.getInstance();
		manager.runTask(httpPost);
	}
	
	public void testDownload() {
		
		PHttpRequestGet requestFileSize = new PHttpRequestGet("http://www.baidu.com/", null);
		requestFileSize.setRequestSocketTimeOut(20000);
		HttpResponse httpResponse = requestFileSize.executeHttpRequest();
		long filemaxsize = (null != httpResponse) ? httpResponse.getEntity().getContentLength() : 0;
		
		String cachepath = Environment.getExternalStorageDirectory().getPath();
		Log.e("test download", cachepath);
		
		PHttpRequestDownloadGet downloadget = new PHttpRequestDownloadGet("http://www.baidu.com/", null, 0, filemaxsize);
		downloadget.setRequestSocketTimeOut(20000);
		downloadget.setDownloadFilePath(cachepath+"/te.txt");
		downloadget.setLocalSize(0);
		downloadget.setFileSize(filemaxsize);
		downloadget.setDownloadFileCallback(new PHttpDownloadFileCallback() {
			
			@Override
			public void requestStart(String localkey, long filesize,
					String downloadFilePath) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void requestReceiveBytes(PHttpRequestDownloadGet request, long num) {
				// TODO Auto-generated method stub
//				request.setCancel(true);
			}
			
			@Override
			public void requestReadInputStreamFinish(boolean isCancel,
					String downloadFilePath) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void requestReadInputStreamFailed(String errorCode,
					String downloadFilePath) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void reqeustProgress(long filesize, float progress) {
				// TODO Auto-generated method stub
				
			}
		});
		
		PHttpRequestManager manager = PHttpRequestManager.getInstance();
		manager.runTask(downloadget);
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
